namespace DotNetNuke.UI.Containers
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Entities.Modules.Actions;
    using DotNetNuke.Entities.Users;
    using DotNetNuke.Security;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Services.Localization;
    using DotNetNuke.Services.Log.EventLog;
    using DotNetNuke.UI.Skins;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;

    /// -----------------------------------------------------------------------------
    /// Project	 : DotNetNuke
    /// Namespace: DotNetNuke.UI.Containers
    /// Class	 : ActionBase
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// ActionBase is the base for the Action objects 
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// [cnurse]	10/07/2004	Documented
    /// </history>
    /// -----------------------------------------------------------------------------
    public abstract class ActionBase : SkinObjectBase
    {
        protected bool m_adminControl;
        protected bool m_adminModule;
        protected bool m_editMode;
        protected ModuleAction m_menuActionRoot;
        protected ModuleActionCollection m_menuActions;
        protected DotNetNuke.Entities.Modules.ModuleInfo m_moduleConfiguration;
        protected PortalModuleBase m_portalModule;
        protected bool m_supportsIcons;
        protected bool m_tabPreview;

        public event ActionEventHandler Action;

        public ActionBase()
        {
            base.Load += new EventHandler(this.Page_Load);
            base.Init += new EventHandler(this.Page_Init);
            this.m_tabPreview = false;
            this.m_editMode = false;
            this.m_adminModule = false;
            this.m_adminControl = false;
            this.m_supportsIcons = true;
        }

        /// -----------------------------------------------------------------------------
        /// <summary></summary>
        /// <remarks></remarks>
        /// <history>
        /// [Nik Kalyani]	10/15/2004	Replaced Optional parameters with multiple method signatures
        /// </history>
        /// -----------------------------------------------------------------------------
        public void AddAction(string Title, string CmdName)
        {
            this.AddAction(Title, CmdName, "", "", "", false, SecurityAccessLevel.Anonymous, false, false);
        }

        public void AddAction(string Title, string CmdName, string CmdArg)
        {
            this.AddAction(Title, CmdName, CmdArg, "", "", false, SecurityAccessLevel.Anonymous, false, false);
        }

        public void AddAction(string Title, string CmdName, string CmdArg, string Icon)
        {
            this.AddAction(Title, CmdName, CmdArg, Icon, "", false, SecurityAccessLevel.Anonymous, false, false);
        }

        public void AddAction(string Title, string CmdName, string CmdArg, string Icon, string Url)
        {
            this.AddAction(Title, CmdName, CmdArg, Icon, Url, false, SecurityAccessLevel.Anonymous, false, false);
        }

        public void AddAction(string Title, string CmdName, string CmdArg, string Icon, string Url, bool UseActionEvent)
        {
            this.AddAction(Title, CmdName, CmdArg, Icon, Url, UseActionEvent, SecurityAccessLevel.Anonymous, false, false);
        }

        public void AddAction(string Title, string CmdName, string CmdArg, string Icon, string Url, bool UseActionEvent, SecurityAccessLevel Secure)
        {
            this.AddAction(Title, CmdName, CmdArg, Icon, Url, UseActionEvent, Secure, false, false);
        }

        public void AddAction(string Title, string CmdName, string CmdArg, string Icon, string Url, bool UseActionEvent, SecurityAccessLevel Secure, bool Visible)
        {
            this.AddAction(Title, CmdName, CmdArg, Icon, Url, UseActionEvent, Secure, Visible, false);
        }

        public void AddAction(string Title, string CmdName, string CmdArg, string Icon, string Url, bool UseActionEvent, SecurityAccessLevel Secure, bool Visible, bool NewWindow)
        {
            ModuleAction Action = this.MenuActions.Add(this.GetNextActionID(), Title, CmdName, CmdArg, Icon, Url, UseActionEvent, Secure, Visible, NewWindow);
        }

        private void ClearCache(ModuleAction Command)
        {
            this.m_portalModule.SynchronizeModule();
            this.Response.Redirect(this.Request.RawUrl, true);
        }

        private void Delete(ModuleAction Command)
        {
            ModuleController objModules = new ModuleController();
            DotNetNuke.Entities.Modules.ModuleInfo objModule = objModules.GetModule(int.Parse(Command.CommandArgument), this.m_portalModule.TabId);
            if (objModule != null)
            {
                objModules.DeleteTabModule(this.m_portalModule.TabId, int.Parse(Command.CommandArgument));
                UserInfo m_UserInfo = UserController.GetCurrentUserInfo();
                new EventLogController().AddLog(objModule, this.PortalSettings, m_UserInfo.UserID, "", EventLogController.EventLogType.MODULE_SENT_TO_RECYCLE_BIN);
            }
            this.Response.Redirect(this.Request.RawUrl, true);
        }

        private void DoAction(ModuleAction Command)
        {
            if (Command.NewWindow)
            {
                this.Response.Write("<script>window.open('" + Command.Url + "','_blank')</script>");
            }
            else
            {
                this.Response.Redirect(Command.Url, true);
            }
        }

        public ModuleAction GetAction(int Index)
        {
            return this.GetAction(Index, this.m_menuActionRoot);
        }

        public ModuleAction GetAction(int Index, ModuleAction ParentAction)
        {
            if (ParentAction != null)
            {
                IEnumerator refObjectHelperL0=null;
                try
                {
                    refObjectHelperL0 = ParentAction.Actions.GetEnumerator();
                    while (refObjectHelperL0.MoveNext())
                    {
                        ModuleAction modaction = (ModuleAction) refObjectHelperL0.Current;
                        if (modaction.ID == Index)
                        {
                            return modaction;
                        }
                        if (modaction.HasChildren())
                        {
                            ModuleAction ChildAction = this.GetAction(Index, modaction);
                            if (ChildAction != null)
                            {
                                return ChildAction;
                            }
                        }
                    }
                }
                finally
                {
                    if (refObjectHelperL0 is IDisposable)
                    {
                        (refObjectHelperL0 as IDisposable).Dispose();
                    }
                }
            }
            return null;
        }

        protected int GetNextActionID()
        {
            return this.GetNextActionID(this.m_menuActionRoot, 0);
        }

        protected int GetNextActionID(ModuleAction ModAction, int Level)
        {
            return ((ModAction.Actions.Count + (ModAction.ID * ((int) Math.Round(Math.Pow(10.0, (double) Level))))) + 1);
        }

        private void MoveToPane(ModuleAction Command)
        {
            ModuleController objModules = new ModuleController();
            objModules.UpdateModuleOrder(this.m_portalModule.TabId, this.m_portalModule.ModuleConfiguration.ModuleID, -1, Command.CommandArgument);
            objModules.UpdateTabModuleOrder(this.m_portalModule.TabId, this.m_portalModule.PortalId);
            this.Response.Redirect(this.Request.RawUrl, true);
        }

        private void MoveUpDown(ModuleAction Command)
        {
            ModuleController objModules = new ModuleController();
            switch (Command.CommandName)
            {
                case "MoveTop.Action":
                    objModules.UpdateModuleOrder(this.m_portalModule.TabId, this.m_portalModule.ModuleConfiguration.ModuleID, 0, Command.CommandArgument);
                    break;

                case "MoveUp.Action":
                    objModules.UpdateModuleOrder(this.m_portalModule.TabId, this.m_portalModule.ModuleConfiguration.ModuleID, this.m_portalModule.ModuleConfiguration.ModuleOrder - 3, Command.CommandArgument);
                    break;

                case "MoveDown.Action":
                    objModules.UpdateModuleOrder(this.m_portalModule.TabId, this.m_portalModule.ModuleConfiguration.ModuleID, this.m_portalModule.ModuleConfiguration.ModuleOrder + 3, Command.CommandArgument);
                    break;

                case "MoveBottom.Action":
                    objModules.UpdateModuleOrder(this.m_portalModule.TabId, this.m_portalModule.ModuleConfiguration.ModuleID, (this.m_portalModule.ModuleConfiguration.PaneModuleCount * 2) + 1, Command.CommandArgument);
                    break;
            }
            objModules.UpdateTabModuleOrder(this.m_portalModule.TabId, this.m_portalModule.PortalId);
            this.Response.Redirect(this.Request.RawUrl, true);
        }

        protected virtual void OnAction(ActionEventArgs e)
        {
            ActionEventHandler refObjectHelperS0 = this.Action;
            if (refObjectHelperS0 != null)
            {
                refObjectHelperS0(this, e);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Init runs when the class is initialised
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	05/12/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        private void Page_Init(object sender, EventArgs e)
        {
            this.m_portalModule = DotNetNuke.UI.Containers.Container.GetPortalModuleBase(this);
            if ((this.Request.Cookies["_Tab_Admin_Preview" + this.m_portalModule.PortalSettings.PortalId.ToString()] != null) && !this.m_portalModule.PortalSettings.ActiveTab.IsAdminTab)
            {
                this.m_tabPreview = bool.Parse(this.Request.Cookies["_Tab_Admin_Preview" + this.m_portalModule.PortalSettings.PortalId.ToString()].Value);
            }
            this.m_adminControl = DotNetNuke.Common.Globals.IsAdminControl();
            if (this.m_portalModule.ModuleConfiguration != null)
            {
                this.m_editMode = PortalSecurity.IsInRoles(this.m_portalModule.PortalSettings.AdministratorRoleName) | PortalSecurity.IsInRoles(this.m_portalModule.PortalSettings.ActiveTab.AdministratorRoles.ToString());
                this.m_adminModule = this.m_portalModule.ModuleConfiguration.IsAdmin;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the class is loaded
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	05/12/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        private void Page_Load(object sender, EventArgs e)
        {
            try
            {
                int intItem;
                this.m_menuActionRoot = new ModuleAction(0, " ", "", "", "action.gif");
                int refIntHelperL0 = this.MenuActions.Count - 1;
                for (intItem = 0; intItem <= refIntHelperL0; intItem++)
                {
                    ModuleAction userAction = this.MenuActions[intItem];
                    if (userAction.Icon == "")
                    {
                        userAction.Icon = "edit.gif";
                    }
                    this.m_menuActionRoot.Actions.Insert(0, new ModuleAction(this.GetNextActionID(), userAction.Title, userAction.CommandName, userAction.CommandArgument, userAction.Icon, userAction.Url, userAction.ClientScript, userAction.UseActionEvent, userAction.Secure, userAction.Visible, userAction.NewWindow));
                }
                if ((this.m_portalModule.ModuleConfiguration.ControlType != SecurityAccessLevel.Anonymous) & (this.Request.QueryString["ctl"] != "Help"))
                {
                    this.SetHelpVisibility();
                }
                if (this.m_portalModule.ModuleConfiguration.DisplayPrint)
                {
                    this.m_menuActionRoot.Actions.Add(this.GetNextActionID(), DotNetNuke.Services.Localization.Localization.GetString("PrintModule.Action", "~/App_GlobalResources/GlobalResources.resx"), "PrintModule.Action", "", "print.gif", DotNetNuke.Common.Globals.NavigateURL(this.m_portalModule.TabId, "", new string[] { "mid=" + this.m_portalModule.ModuleId.ToString(), "SkinSrc=" + DotNetNuke.Common.Globals.QueryStringEncode("[G]" + SkinInfo.RootSkin + "/_default/No Skin"), "ContainerSrc=" + DotNetNuke.Common.Globals.QueryStringEncode("[G]" + SkinInfo.RootContainer + "/_default/No Container"), "dnnprintmode=true" }), "", false, SecurityAccessLevel.Anonymous, true, true);
                }
                if ((this.m_editMode & !this.m_adminModule) & !this.m_adminControl)
                {
                    IEnumerator refObjectHelperL0=null;
                    this.m_menuActionRoot.Actions.Add(this.GetNextActionID(), "~", "", "", "", "", false, SecurityAccessLevel.Anonymous, true, false);
                    this.m_menuActionRoot.Actions.Add(this.GetNextActionID(), DotNetNuke.Services.Localization.Localization.GetString("ModuleSettings.Action", "~/App_GlobalResources/GlobalResources.resx"), "ModuleSettings.Action", "", "settings.gif", DotNetNuke.Common.Globals.NavigateURL(this.m_portalModule.TabId, "Module", new string[] { "ModuleId=" + this.m_portalModule.ModuleId.ToString() }), false, SecurityAccessLevel.Admin, true, false);
                    this.m_menuActionRoot.Actions.Add(this.GetNextActionID(), DotNetNuke.Services.Localization.Localization.GetString("DeleteModule.Action", "~/App_GlobalResources/GlobalResources.resx"), "DeleteModule.Action", this.m_portalModule.ModuleConfiguration.ModuleID.ToString(), "delete.gif", "", DotNetNuke.Services.Localization.Localization.GetString("DeleteModule.Confirm"), false, SecurityAccessLevel.Admin, true, false);
                    if (this.m_portalModule.ModuleConfiguration.CacheTime != 0)
                    {
                        this.m_menuActionRoot.Actions.Add(this.GetNextActionID(), DotNetNuke.Services.Localization.Localization.GetString("ClearCache.Action", "~/App_GlobalResources/GlobalResources.resx"), "ClearCache.Action", this.m_portalModule.ModuleConfiguration.ModuleID.ToString(), "restore.gif", "", false, SecurityAccessLevel.Admin, true, false);
                    }
                    this.m_menuActionRoot.Actions.Add(this.GetNextActionID(), "~", "", "", "", "", false, SecurityAccessLevel.Anonymous, true, false);
                    ModuleAction MoveActionRoot = new ModuleAction(this.GetNextActionID(), DotNetNuke.Services.Localization.Localization.GetString("MoveRoot.Action", "~/App_GlobalResources/GlobalResources.resx"), "", "", "", "", "", false, SecurityAccessLevel.Admin, this.m_editMode);
                    if (this.m_portalModule.ModuleConfiguration != null)
                    {
                        this.SetMoveMenuVisibility(MoveActionRoot.Actions.Add(this.GetNextActionID(MoveActionRoot, 1), DotNetNuke.Services.Localization.Localization.GetString("MoveTop.Action", "~/App_GlobalResources/GlobalResources.resx"), "MoveTop.Action", this.m_portalModule.ModuleConfiguration.PaneName, "top.gif", "", false, SecurityAccessLevel.Admin, this.m_editMode, false));
                        this.SetMoveMenuVisibility(MoveActionRoot.Actions.Add(this.GetNextActionID(MoveActionRoot, 1), DotNetNuke.Services.Localization.Localization.GetString("MoveUp.Action", "~/App_GlobalResources/GlobalResources.resx"), "MoveUp.Action", this.m_portalModule.ModuleConfiguration.PaneName, "up.gif", "", false, SecurityAccessLevel.Admin, this.m_editMode, false));
                        this.SetMoveMenuVisibility(MoveActionRoot.Actions.Add(this.GetNextActionID(MoveActionRoot, 1), DotNetNuke.Services.Localization.Localization.GetString("MoveDown.Action", "~/App_GlobalResources/GlobalResources.resx"), "MoveDown.Action", this.m_portalModule.ModuleConfiguration.PaneName, "dn.gif", "", false, SecurityAccessLevel.Admin, this.m_editMode, false));
                        this.SetMoveMenuVisibility(MoveActionRoot.Actions.Add(this.GetNextActionID(MoveActionRoot, 1), DotNetNuke.Services.Localization.Localization.GetString("MoveBottom.Action", "~/App_GlobalResources/GlobalResources.resx"), "MoveBottom.Action", this.m_portalModule.ModuleConfiguration.PaneName, "bottom.gif", "", false, SecurityAccessLevel.Admin, this.m_editMode, false));
                    }
                    int refIntHelperL2 = this.m_portalModule.PortalSettings.ActiveTab.Panes.Count - 1;
                    for (intItem = 0; intItem <= refIntHelperL2; intItem++)
                    {
                        this.SetMoveMenuVisibility(MoveActionRoot.Actions.Add(this.GetNextActionID(MoveActionRoot, 2), DotNetNuke.Services.Localization.Localization.GetString("MovePane.Action", "~/App_GlobalResources/GlobalResources.resx") + " " + Convert.ToString(RuntimeHelpers.GetObjectValue(this.m_portalModule.PortalSettings.ActiveTab.Panes[intItem])), "MovePane.Action", Convert.ToString(RuntimeHelpers.GetObjectValue(this.m_portalModule.PortalSettings.ActiveTab.Panes[intItem])), "move.gif", "", false, SecurityAccessLevel.Admin, this.m_editMode, false));
                    }
                    try
                    {
                        refObjectHelperL0 = MoveActionRoot.Actions.GetEnumerator();
                        while (refObjectHelperL0.MoveNext())
                        {
                            ModuleAction ma = (ModuleAction) refObjectHelperL0.Current;
                            if (ma.Visible)
                            {
                                this.m_menuActionRoot.Actions.Add(MoveActionRoot);
                                return;
                            }
                        }
                    }
                    finally
                    {
                        if (refObjectHelperL0 is IDisposable)
                        {
                            (refObjectHelperL0 as IDisposable).Dispose();
                        }
                    }
                }
            }
            catch (Exception exception1)
            {
                
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
                
            }
        }

        public void ProcessAction(string ActionID)
        {
            if (Versioned.IsNumeric(ActionID))
            {
                ModuleAction action = this.GetAction(Convert.ToInt32(ActionID));
                string refStringHelperL0 = action.CommandName;
                switch (refStringHelperL0)
                {
                    case "ModuleHelp.Action":
                        this.DoAction(action);
                        return;

                    case "OnlineHelp.Action":
                        this.DoAction(action);
                        return;

                    case "ModuleSettings.Action":
                        this.DoAction(action);
                        return;

                    case "DeleteModule.Action":
                        this.Delete(action);
                        return;

                    case "PrintModule.Action":
                        this.DoAction(action);
                        return;

                    case "ClearCache.Action":
                        this.ClearCache(action);
                        return;

                    case "MovePane.Action":
                        this.MoveToPane(action);
                        return;
                }
                if (((refStringHelperL0 == "MoveBottom.Action") ? 1 : 0) != 0)
                {
                    this.MoveUpDown(action);
                }
                else if ((action.Url.Length > 0) & !action.UseActionEvent)
                {
                    this.DoAction(action);
                }
                else
                {
                    this.OnAction(new ActionEventArgs(action, this.ModuleConfiguration));
                }
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// SetHelp Adds the Help icons to the Action Menu
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	05/12/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        private void SetHelpVisibility()
        {
            ModuleAction helpAction = new ModuleAction(this.GetNextActionID());
            helpAction.Title = DotNetNuke.Services.Localization.Localization.GetString("ModuleHelp.Action", "~/App_GlobalResources/GlobalResources.resx");
            helpAction.CommandName = "ModuleHelp.Action";
            helpAction.CommandArgument = "";
            helpAction.Icon = "help.gif";
            helpAction.Url = DotNetNuke.Common.Globals.NavigateURL(this.m_portalModule.TabId, "Help", new string[] { "ctlid=" + this.m_portalModule.ModuleConfiguration.ModuleControlId.ToString(), "moduleid=" + Conversions.ToString(this.m_portalModule.ModuleId) });
            helpAction.Secure = SecurityAccessLevel.Edit;
            helpAction.Visible = true;
            helpAction.NewWindow = false;
            this.m_menuActionRoot.Actions.Add(helpAction);
            string helpUrl = DotNetNuke.Common.Globals.GetOnLineHelp(this.m_portalModule.ModuleConfiguration.HelpUrl, this.m_portalModule.ModuleConfiguration);
            if (!Null.IsNull(helpUrl))
            {
                helpAction = new ModuleAction(this.GetNextActionID());
                helpAction.Title = DotNetNuke.Services.Localization.Localization.GetString("OnlineHelp.Action", "~/App_GlobalResources/GlobalResources.resx");
                helpAction.CommandName = "OnlineHelp.Action";
                helpAction.CommandArgument = "";
                helpAction.Icon = "help.gif";
                helpAction.Url = DotNetNuke.Common.Globals.FormatHelpUrl(helpUrl, this.m_portalModule.PortalSettings, this.m_portalModule.ModuleConfiguration.FriendlyName);
                helpAction.Secure = SecurityAccessLevel.Edit;
                helpAction.UseActionEvent = true;
                helpAction.Visible = true;
                helpAction.NewWindow = true;
                this.m_menuActionRoot.Actions.Add(helpAction);
            }
        }

        private void SetMoveMenuVisibility(ModuleAction Action)
        {
            switch (Action.CommandName)
            {
                case "MoveTop.Action":
                    Action.Visible = ((this.m_portalModule.ModuleConfiguration.ModuleOrder != 0) & (this.m_portalModule.ModuleConfiguration.PaneModuleIndex > 0)) & this.m_editMode;
                    break;

                case "MoveUp.Action":
                    Action.Visible = ((this.m_portalModule.ModuleConfiguration.ModuleOrder != 0) & (this.m_portalModule.ModuleConfiguration.PaneModuleIndex > 0)) & this.m_editMode;
                    break;

                case "MoveDown.Action":
                    Action.Visible = ((this.m_portalModule.ModuleConfiguration.ModuleOrder != 0) & (this.m_portalModule.ModuleConfiguration.PaneModuleIndex < (this.m_portalModule.ModuleConfiguration.PaneModuleCount - 1))) & this.m_editMode;
                    break;

                case "MoveBottom.Action":
                    Action.Visible = ((this.m_portalModule.ModuleConfiguration.ModuleOrder != 0) & (this.m_portalModule.ModuleConfiguration.PaneModuleIndex < (this.m_portalModule.ModuleConfiguration.PaneModuleCount - 1))) & this.m_editMode;
                    break;

                case "MovePane.Action":
                    Action.Visible = (Strings.LCase(this.m_portalModule.ModuleConfiguration.PaneName) != Strings.LCase(Action.CommandArgument)) & this.m_editMode;
                    break;
            }
        }

        public ModuleAction ActionRoot
        {
            get
            {
                return this.m_menuActionRoot;
            }
        }

        public bool EditMode
        {
            get
            {
                return this.m_editMode;
            }
        }

        public ModuleActionCollection MenuActions
        {
            get
            {
                if (this.m_menuActions == null)
                {
                    this.m_menuActions = new ModuleActionCollection();
                }
                return this.m_menuActions;
            }
        }

        public DotNetNuke.Entities.Modules.ModuleInfo ModuleConfiguration
        {
            get
            {
                return this.m_moduleConfiguration;
            }
            set
            {
                this.m_moduleConfiguration = value;
            }
        }

        public bool SupportsIcons
        {
            get
            {
                return this.m_supportsIcons;
            }
        }
    }
}

